package com.xiyi.shouxin.init;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

import lombok.Getter;

/*
 * 通过延时队列简单处理token过期及练习
 */
@Getter
public class DelayTask implements Delayed {
    private String token;
    private long expiredTime;

    public DelayTask(String token, long duration) {
        this.token = token;
        this.expiredTime = System.currentTimeMillis() + duration;
    }

    @Override
    public int compareTo(Delayed o) {
        if (this == o) {
            return 0;
        }
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expiredTime - System.currentTimeMillis(),TimeUnit.MILLISECONDS);
    }

}
